package cn.jinjimi.app.util;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.jinjimi.app.R;
import cn.jinjimi.app.main.dialog.LoadingDialog;
import cn.jinjimi.base.util.ToastUtils;

/**
 * Created by lforxeverc on 2017/4/25.
 */

public class ToastUtil {
    public static void showLoading(Context context) {
        showLoading(context, "");
    }

    /**
     * show loading
     */
    public static void showLoading(Context context, String msg) {
        LoadingDialog.show(context, msg);
    }

    /**
     * 持续显示loading
     *
     * @param context
     * @param msg
     */
    public static void showLoadingLast(Context context, String msg) {
        LoadingDialog.showLast(context, msg);
    }

    /**
     * hide loading
     */
    public static void hideLoading() {
        LoadingDialog.dispose();
    }


    /**
     * 显示特殊toast
     *
     * @param ctx
     * @param msg
     */
    public static void showCusToast(Context ctx, String msg, int state) {
        Toast toast = new Toast(ctx);
        LayoutInflater inflate = (LayoutInflater)
                ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflate.inflate(R.layout.toast_layout, null);
        TextView tv = (TextView) view.findViewById(R.id.tv_msg);
        ImageView iv = (ImageView) view.findViewById(R.id.iv_icon);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER, 0, 0);
        tv.setText(msg);
        if (state == ToastUtils.SUCCESS) {
            iv.setImageResource(R.drawable.icon_toast_tick);
        } else {
            iv.setImageResource(R.drawable.icon_toast_cross);
        }
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

    public static void showCusToast(Context context, String msg) {
        Toast toast = new Toast(context);
        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflate.inflate(R.layout.toast_layout, null);
        TextView tv = (TextView) view.findViewById(R.id.tv_msg);
        ImageView iv = (ImageView) view.findViewById(R.id.iv_icon);
        iv.setVisibility(View.GONE);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER, 0, 0);
        tv.setText(msg);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }
}
